{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<built-in method isOpened of cv2.VideoCapture object at 0x00000260980DCB50>\n",
      "29.008016032064127 540 960\n",
      "image1.jpg\n",
      "image2.jpg\n",
      "image3.jpg\n",
      "image4.jpg\n",
      "image5.jpg\n",
      "image6.jpg\n",
      "image7.jpg\n",
      "image8.jpg\n",
      "image9.jpg\n",
      "image10.jpg\n",
      "end\n"
     ]
    }
   ],
   "source": [
    "# 视频分解图片步骤\n",
    "# 1.加载视频 2.获取视频信息 3.解析 4.展示 5.保存\n",
    "import cv2\n",
    "# 加载视频\n",
    "cap = cv2.VideoCapture('1.mp4')\n",
    "# 判断视频能否打开\n",
    "isOpened = cap.isOpened\n",
    "print(isOpened)\n",
    "# 获取视频信息(帧率以及宽度和高度)\n",
    "fps = cap.get(cv2.CAP_PROP_FPS)\n",
    "width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))\n",
    "height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))\n",
    "print(fps,width,height)\n",
    "i = 0\n",
    "# 读取10帧\n",
    "while(isOpened):\n",
    "    if i == 10:\n",
    "        break\n",
    "    else:\n",
    "        i = i + 1\n",
    "        #读取图片 flag返回是否读取成功 frame为读取到的图片内容\n",
    "        (flag,frame) = cap.read()\n",
    "        fileName = 'image'+str(i)+'.jpg'\n",
    "        print(fileName)\n",
    "        # 如果图片读取成功则保存\n",
    "        if flag == True:\n",
    "            # 参数3：图片保存的质量，100代表质量最高\n",
    "            cv2.imwrite(fileName,frame,[cv2.IMWRITE_JPEG_QUALITY,100])\n",
    "print(\"end\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
